import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/home/vs',
    component: Home,
    children: [
      { path: 'vs', component: () => import(/* webpackChunkName: "Home" */'../views/vs') },
      { path: 'category', component: () => import(/* webpackChunkName: "Home" */'../views/category') },
      { path: 'env', component: () => import(/* webpackChunkName: "Home" */'../views/env') },
      { path: 'model', component: () => import(/* webpackChunkName: "Home" */'../views/model') },
      { path: 'train', component: () => import(/* webpackChunkName: "Home" */'../views/train') },
      { path: 'replay', component: () => import(/* webpackChunkName: "Home" */'../views/replay') }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */'../views/login')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !localStorage.getItem('vs_token')) {
    next({ path: '/login' })
  } else if (to.path === '/login' && localStorage.getItem('vs_token')) {
    next({ path: '/' })
  } else {
    next()
  }
})

export default router
